<?php
// +----------------------------------------------------------------------
// | xiongrui [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.xiongrui.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: molong <molong@tensent.cn> <http://www.xiongrui.cn>
// +----------------------------------------------------------------------

namespace app\index\controller;
use app\common\controller\Fornt;
use app\common\model\Coupon as CouponModel;

class Game extends Fornt{

	public function _initialize(){
		parent::_initialize();
		$script = &  load_wechat('Script');
    // 获取JsApi使用签名，通常这里只需要传 $ur l参数
    $url = $this->request->url(true);
    $options = $script->getJsSign($url);

    // 处理执行结果
    if($options===FALSE){
    	// 接口失败的处理
        echo $script->errMsg;
    }
		$this->assign('options', $options);
    $this->assign('crurl', $url);

	}

	public function dzp(){
    //每人每日抽奖一次，查询今天是否抽过奖
    $arr = ['user_id'=>USER_ID];
		$info = db('GameDzphistory')->where($arr)->whereTime('create_time', 'today')->select();
		if($info){
			$degree = 0;
		}else{
			$degree = 1;
		}
		$this->assign('degree',$degree);
    return $this->fetch();
	}

  /*
  添加优惠券
   */
  public function addCoupon($total, $price){
    $time=time()+30*24*3600;   //30天后过期
    $couponData = ['coupon_id'=>3,'uid'=>USER_ID,'code'=>date('md').rand_number(4),'total'=>$total,'price'=>$price,'deadline'=>$time];
    $return = db('CouponList')->insertGetId($couponData);


		if($return){
			//插入行为日志
			$data['action_id']   = 13; //行为id为 13  后台生成
			$data['user_id']     = USER_ID;
			$data['action_ip']   = ip2long(get_client_ip());
			$data['model']       = 'WxUser';
			$data['record_id']   = $return;
			$data['create_time'] = time();
			$data['remark'] = time_format($data['create_time']).'大转盘游戏获得'.$price.'元优惠券';
			db('ActionLog')->insert($data);
		}
  }
  /*
  添加积分
   */
  public function addIntegral($total, $text){
		//添加积分
		$total = ceil($total);
		db('WxUser')->where('id',USER_ID)->setInc('integral', $total);
		if($total == 0){
			$remark = time_format($data['create_time']).'大转盘游戏获得谢谢惠顾';
		}else{
			$remark = time_format($data['create_time']).'抽奖游戏'.$text.'获得'.$total.'积分';
		}
		//插入行为日志
		$data['user_id']     = USER_ID;
		$data['action_ip']   = ip2long(get_client_ip());
		$data['create_time'] = time();
		$data['remark'] = $remark;
		$data['status'] = 1;
		db('GameDzphistory')->insert($data);

		if($total != 0){
			db('ActionLog')->insert($data);
		}
  }
  public function addTest($jp, $text){

			$remark = time_format($data['create_time']).'抽奖游戏'.$text.'获得奖品：'.$jp;
		//插入行为日志
		$data['user_id']     = USER_ID;
		$data['action_ip']   = ip2long(get_client_ip());
		$data['create_time'] = time();
		$data['remark'] = $remark;
		db('GameDzphistory')->insert($data);

  }
	/**
	 * 添加余额
	 * @param [type] $total [description]
	 * @param [type] $text  [description]
	 */
	public function addBalance($total, $text){
		$total = floatval($total);
		db('WxUser')->where('id',USER_ID)->setInc('balance', $total);
		if($total == 0){
			$remark = time_format($data['create_time']).'大转盘游戏获得谢谢惠顾';
		}else{
			$remark = time_format($data['create_time']).'抽奖游戏'.$text.'获得'.$total.'元';
		}
		//插入行为日志
		$data['user_id']     = USER_ID;
		$data['action_ip']   = ip2long(get_client_ip());
		$data['create_time'] = time();
		$data['remark'] = $remark;
		$data['status'] = 1;
		db('GameDzphistory')->insert($data);

		if($total != 0){
			db('ActionLog')->insert($data);
		}
	}

	protected function addDate($data, $text, $type){
		if($type == 0){
			//添加积分
			$this->addIntegral($data, $text);
		}elseif ($type == 2) {
			//添加余额
			$this->addBalance($data, $text);
		}else {
			//添加文字
			$this->addTest($data, $text);
		}
	}


	public function rnd(){
		$total = config('ydjsl')+config('edjsl')+config('sdjsl')+config('sidjsl')+config('wdjsl')+config('xxhgsl');
		$y = config('ydjsl');
		$e = config('ydjsl')+config('edjsl');
		$s = config('ydjsl')+config('edjsl')+config('sdjsl');
		$si = config('ydjsl')+config('edjsl')+config('sdjsl')+config('sidjsl');
		$w = config('ydjsl')+config('edjsl')+config('sdjsl')+config('sidjsl')+config('wdjsl');



		$time = date('y-m-d',time());
		$GD = model('game_dzptimeinfo');
		$timeinfo = $GD->where('time',$time)->find();


		if($timeinfo){

		}else{
			for ($i=1; $i <$total.length ; $i++) {
				$ydata .= $i.',';
			}
			$GD->insert(['time'=>$time, 'ydata'=>$ydata]);
		}

		$timeinfo = $GD->where('time',$time)->find();
		$ydata = explode(',',$timeinfo['ydata']);

		if(!empty($timeinfo['ydata'])){
			$rand_ydata = array_rand($ydata,1);
			$rand_number = $ydata[$rand_ydata];
			unset($ydata[$rand_ydata]);
			$ydata = implode(',',$ydata);
		}else{
			$ydata = '';
			$rand_number = -1;
		}




		$GD->where('time',$time)->update(['info'=>$timeinfo['info'].','.$rand_number, 'ydata'=>$ydata]);


		if($rand_number==$y){
			$jp = 1;
			$this->addDate(config('ydjjp'), '一等奖', config('ydjlx'));
		}elseif ($rand_number>$y&&$rand_number<=$e) {
			$jp = 2;
			$this->addDate(config('edjjp') , '二等奖', config('edjlx'));
		}elseif ($rand_number>$e&&$rand_number<=$s) {
			$jp = 3;
			$this->addDate(config('sdjjp') , '三等奖', config('sdjlx'));
		}elseif ($rand_number>$s&&$rand_number<=$si) {
			$jp = 4;
			$this->addDate(config('sidjjp') , '四等奖', config('sidjlx'));
		}elseif ($rand_number>$si&&$rand_number<=$w) {
			$jp = 5;
			$this->addDate(config('wdjjp') , '五等奖', config('wdjlx'));
		}else{
			$jp = -1;
			$this->addIntegral(0,'');
		}

		update_session(USER_ID);
		return $jp;

	}


	public function history(){
		$arr = ['user_id'=>USER_ID];
		$info = db('GameDzphistory')->where($arr)->order('id desc')->paginate(10);
		$this->assign('list', $info);
		return $this->fetch();
	}

	//实体抽奖，不限次数
	public function stdzp(){
		//每人每日抽奖一次，查询今天是否抽过奖
    $arr = ['user_id'=>USER_ID];
		$info = db('GameDzphistory')->where($arr)->whereTime('create_time', 'today')->select();
		if($info){
			$degree = 0;
		}else{
			$degree = 1;
		}
		$this->assign('degree',$degree);
    return $this->fetch();
	}


	public function strnd(){
		$total = config('st_ydjsl')+config('st_edjsl')+config('st_sdjsl')+config('st_sidjsl')+config('st_wdjsl')+config('st_xxhgsl');
		$y = config('st_ydjsl');
		$e = config('st_ydjsl')+config('st_edjsl');
		$s = config('st_ydjsl')+config('st_edjsl')+config('st_sdjsl');
		$si = config('st_ydjsl')+config('st_edjsl')+config('st_sdjsl')+config('st_sidjsl');
		$w = config('st_ydjsl')+config('st_edjsl')+config('st_sdjsl')+config('st_sidjsl')+config('st_wdjsl');



		$time = date('y-m-d',time());
		$GD = model('game_dzptimeinfo');
		$timeinfo = $GD->where('time',$time)->find();


		if($timeinfo){

		}else{
			for ($i=1; $i <$total.length ; $i++) {
				$ydata .= $i.',';
			}
			$GD->insert(['time'=>$time, 'ydata'=>$ydata]);
		}

		$timeinfo = $GD->where('time',$time)->find();
		$ydata = explode(',',$timeinfo['ydata']);

		if(!empty($timeinfo['ydata'])){
			$rand_ydata = array_rand($ydata,1);
			$rand_number = $ydata[$rand_ydata];
			unset($ydata[$rand_ydata]);
			$ydata = implode(',',$ydata);
		}else{
			$ydata = '';
			$rand_number = -1;
		}




		$GD->where('time',$time)->update(['info'=>$timeinfo['info'].','.$rand_number, 'ydata'=>$ydata]);


		if($rand_number==$y){
			$jp = 1;
			$this->addDate(config('st_ydjjp'), '一等奖(实体店)', config('st_ydjlx'));
		}elseif ($rand_number>$y&&$rand_number<=$e) {
			$jp = 2;
			$this->addDate(config('st_edjjp'), '二等奖(实体店)', config('st_edjlx'));
		}elseif ($rand_number>$e&&$rand_number<=$s) {
			$jp = 3;
			$this->addDate(config('st_sdjjp'), '三等奖(实体店)', config('st_sdjlx'));
		}elseif ($rand_number>$s&&$rand_number<=$si) {
			$jp = 4;
			$this->addDate(config('st_sidjjp'), '四等奖(实体店)', config('st_sidjlx'));
		}elseif ($rand_number>$si&&$rand_number<=$w) {
			$jp = 5;
			$this->addDate(config('st_wdjjp'), '五等奖(实体店)', config('st_wdjlx'));
		}else{
			$jp = -1;
			$this->addIntegral(0,'');
		}
		update_session(USER_ID);
		return $jp;

	}


}
